#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/9/22  10:42
# @Author: 余浪人
# @email: yulangren520@gmail.com
from flask import request, render_template
from flask_login import login_required
from apps.control import sys_bp
from apps.lib.public import page_auto, real_delete_data, real_deletes
from apps.models.content_model import TL_Comment_Model, db, TL_Content_Model


# 评论列表
@sys_bp.route('/comment_list', endpoint='comment_list', methods=['GET', 'DELETE', 'DELETES'])
@login_required
def content_list():
    if request.method == 'DELETE':
        return real_delete_data(TL_Comment_Model, db, request.form.get('uid'))
    elif request.method == 'DELETES':
        return real_deletes(TL_Comment_Model, db, request.form.get('id_array').split('#TL#'))
    data_list, page = page_auto(
        TL_Comment_Model.query.order_by(TL_Comment_Model.id.desc()).filter(TL_Comment_Model.status == 1))
    search = request.args.get('search', '').strip()
    if search:
        c = TL_Content_Model.query.filter(TL_Content_Model.title == search).first()
        if c:
            data_list, page = page_auto(
                TL_Comment_Model.query.order_by(TL_Comment_Model.id.desc()).filter(TL_Comment_Model.status == 1, TL_Comment_Model.content_uuid == c.uuid))
        else:
            data_list = []
    if len(data_list):
        data_list = [val for val in data_list if val.pro]
    return render_template('system_template/comment_list.html', data_list=data_list, page=page, search=search)
